<?php 

class Tags extends CI_Model {
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
  	 function load_by_id($id)
    {
    }	    
	function load_tags($tag_list_or_id, $doload = true)
	{
		//The trick here is the $tag_list_or_id can be string with comma separated values, or 
		// a single string or numeric values or a list.
		if($doload)
		{		
			$criteria = '0'; //This will be a comma separated list. We are adding a zero to make comma
									// list easier to make. This can be fixed later.
			
			if(is_array($tag_list_or_id))
			{
				foreach($tag_list_or_id as $t)
				{
					if(is_valid_location_index($t))
					{
						$criteria .= ',' . (string)$t;
					}
					else
					{
						var_dump($tag_list_or_id);
						show_error('Tag id index array contains something that is not a location index.');
					}
				}
			}
			else if(empty($tag_list_or_id))
			{
				return array();
			}
			else if(is_valid_location_index($tag_list_or_id) && (!(strpos($tag_list_or_id,","))))
			{
				$criteria = (string)(intval($tag_list_or_id));
			}
			else if(is_string($tag_list_or_id))
			{
				$tag_string_array = explode(',', $tag_list_or_id);
				foreach($tag_string_array as $tag_str)
				{
					if(!is_valid_location_index($tag_str))
					{
						var_dump($tag_list_or_id);
						show_error('Tag id is not a numeric value, and it must be a numeric value');
					}
					$criteria .= ',' . (string) intval($tag_str);
				}
			}
			else
			{
				var_dump($tag_list_or_id);
				show_error('Tag id is something weird.');
			}


			$query = $this->db->from('tags')
					->where('idtags in ('.$criteria.')')
					->get();
					
			return $query->result();	
		}
		else
		{
			$result = array(); //This will be a comma separated list. We are adding a zero to make comma
									// list easier to make. This can be fixed later.
			
			if(is_array($tag_list_or_id))
			{
				foreach($tag_list_or_id as $t)
				{
					if(is_valid_location_index($t))
					{
						$result[] = intval($t);
					}
					else
					{
						var_dump($tag_list_or_id);
						show_error('Tag id index array contains something that is not a location index.');
					}
				}
			}
			else if(empty($tag_list_or_id))
			{
				return $result;
			}
			else if(is_valid_location_index($tag_list_or_id) && (!(strpos($tag_list_or_id,","))))
			{
				$result[] = intval($tag_list_or_id);
			}
			else if(is_string($tag_list_or_id))
			{
				$tag_string_array = explode(',', $tag_list_or_id);
				foreach($tag_string_array as $tag_str)
				{
					if(!is_valid_location_index($tag_str))
					{
						var_dump($tag_list_or_id);
						show_error('Tag id is not a numeric value, and it must be a numeric value');
					}
					$result[] = intval($tag_str);
				}
			}
			else
			{
				var_dump($tag_list_or_id);
				show_error('Tag id is something weird.');
			}	
			return $result;
		}
	}    

	function load_relations()
	{
		$query = $this->db->get('tags_relations');
		return $query->result();
	}
	
	function get_tags($criteria, $limit = 20, $offset = 0)
	{
        $res = $this->db->like('name', $criteria)
					->order_by("idtags", "asc")
					->get('tags', $limit, $offset)->result();
		/*echo 'res '; print_r($res);*/
		return $res;
	}
	function get_random_tags($limit, $offset = 0 )
	{
        $res = $this->db->order_by("idtags", "asc")
					->get('tags', $limit, $offset)->result();
		/*echo 'res '; print_r($res);*/
		return $res;
	}
    
    function create_new($name)
    {
        $this->db->set('name', $name)
             ->insert('tags');

        // retrieve the ID
        $query = $this->db->query('SELECT LAST_INSERT_ID()');
        $row = $query->row_array();
        return $row['LAST_INSERT_ID()'];
    }
}